import requests, random
from lxml import etree
import MySQLdb

# 连接数据库
conn = MySQLdb.connect(
    host="localhost",  # mysql所在的主机的ip
    port=3306,  # mysql的端口号
    user="root",  # mysql的用户名
    password="123456",  # mysql的密码
    db="reptiles",  # 要使用的库名
    charset="utf8",  # 连接中使用的字符集

)
cursor = conn.cursor(MySQLdb.cursors.DictCursor)  # 获取游标对象,里面可以为空,这样写是为了显示全面的信息
user_agent = [
    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
    "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"
]
user_agent = random.choice(user_agent)
cookie = "anonymid=kd4aa8rsbindye; depovince=GW; _r01_=1; taihe_bi_sdk_uid=8e5bdc5da592aaf9e7e59714fa6a7832; ick_login=ee74413b-9078-4f9d-b4f3-95a3a4697c46; taihe_bi_sdk_session=0ae58097fd8b9747945a8d4f4846c1fa; loginfrom=null; ch_id=10050; _ga=GA1.2.891496245.1596025819; _gid=GA1.2.1090431027.1596025819; wp_fold=0; XNESSESSIONID=0ee7e45a1d83; t=bb6bd083ed148802a5c8cc3681a6612a5; societyguester=bb6bd083ed148802a5c8cc3681a6612a5; id=974828435; xnsid=6542987f; ver=7.0; jebecookies=d681250b-c358-4713-a947-df0852f402e5|||||"
headers = {
    "user-agent": user_agent,
    "cookie": cookie,
}

# 手动获取部分更替数据,进行遍历就行(这里就不再接受所有的筛选条件了)
# 需要注意的是 ,name 和id应该是绑定的,所有不要去修改,我们这里去修改后面的值即可
departs = ["三附院", "东方医院", "东方学院", "东直门医院", "中药制药", "中药学院", "中药系", "台港澳中医学部",
           "国际学院", "基础医学院", "护理学院", "教育培训中心", "文法系", "管理学院", "继续教育学院", "高职",
           "针灸学院", "远程教育学院"
           ]
depart = random.choice(departs)
# year可以直接遍历了
types = ["硕士", "博士", "教工", "教师", "大学生"]
type = random.choice(types)
# 这里的name是u的name值
names = ["北京二十四中", "北京一二六中", "北京二十七中", "北京二十五中",
         "北京一六六中", "北京二十一中", "北京七十九中", "北京一九六中",
         "北京一九五中", "东直门中学", "交道口中学", "北京五中分校",
         "北京一中", "北京八十五中", "北京二中", "北京一六四中"
         ]
name = random.choice(names)
print(depart)
print(type)
print(name)

data = {
    # 'p': '[{"name":"北京中医药大学","id":"1023","depart":"基础医学院","year":"2019","type":"大学生","t":"univ"}]',
    "p":"[]",
    'u': '[{"name":"北京一六六中","id":"40000058","year":"2019","t":"juni"}]'
}

url = "http://rcd.renren.com/cwf_nget_guide_lead"

# 注意这个是post请求,所以说需要传输data的值传输过去

res = requests.post(url, headers=headers, params=data).json()
# with open("renren2.html","w",encoding="utf-8") as w:   #有登录信息的cookie,所以直接进去我们的主页面
#     w.write(res)

infors = res["candidate"]  # 获取所有人的信息
print(infors)
for infor in infors:
    # 人人网id,姓名
    r_id = str(infor["id"])
    name = infor["name"]
    sql = "insert into renren values (%s,%s)"
    last = cursor.execute(sql, [name, r_id])
    conn.commit()
cursor.close()
conn.close()

'''
对人人网不熟悉,怎么过来过去就那么几个人..
'''
